package aufagabe2.master;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.TextField;
import org.eclipse.wb.swing.FocusTraversalOnArray;

import akka.actor.ActorRef;

import javax.swing.JButton;
import java.awt.Font;
import java.awt.Label;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class GUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private TextField nrToFact;
	private JPanel panel;
	private int nbworker = 10;
	private ArrayList<JLabel> lblsWorkers = new ArrayList<JLabel>();
	private Label results,sum,cpu,time;
	
	public int getNbworker(){
		return nbworker;
	}
	
	private GUIdata data=new GUIdata();

	/**
	 * Create the frame.J
	 */
	public GUI(final ActorRef master) {
		setTitle("Factorization prime numbers");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 654, 500);
		contentPane = new JPanel();
		contentPane.setBorder(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		panel = new JPanel();
		panel.setBounds(0, 0, 634, 459);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNumberToFactorize = new JLabel("Number to factorize");
		lblNumberToFactorize.setBounds(23, 167, 164, 14);
		panel.add(lblNumberToFactorize);
		lblNumberToFactorize.setHorizontalAlignment(SwingConstants.LEFT);
		contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{lblNumberToFactorize, nrToFact}));
		
		nrToFact = new TextField();
		nrToFact.setFont(null);
		nrToFact.setText("25698742");
		nrToFact.setBounds(23, 187, 192, 22);
		panel.add(nrToFact);
		
		JButton btnFactorize = new JButton("Factorize");
		btnFactorize.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				data.setToFactorize(nrToFact.getText());
				data.setTypeOf("factorize");
				master.tell(data);
			}
		});
		btnFactorize.setBounds(23, 215, 192, 23);
		panel.add(btnFactorize);
		
		JLabel lblMenu = new JLabel("Worker Menu");
		lblMenu.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblMenu.setBounds(23, 11, 85, 14);
		panel.add(lblMenu);
		
		final TextField fieldAddress = new TextField();
		fieldAddress.setText("localhost");
		fieldAddress.setBounds(23, 58, 127, 22);
		panel.add(fieldAddress);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(23, 38, 85, 14);
		panel.add(lblAddress);
		
		final TextField fieldPort = new TextField();
		fieldPort.setText("2552");
		fieldPort.setBounds(169, 58, 46, 22);
		panel.add(fieldPort);
		
		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(173, 38, 46, 14);
		panel.add(lblPort);
		
		final JButton btnAddWorker = new JButton("Add worker");
		btnAddWorker.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(fieldAddress.getText().length()==0 || fieldPort.getText().length()==0 || Integer.parseInt(fieldPort.getText())<=0 )
					JOptionPane.showMessageDialog(null, "At least one of the field is not valid", 
							"Error", JOptionPane.ERROR_MESSAGE);
				else
				{
					data.setAddress(fieldAddress.getText());
					data.setPort(Integer.parseInt(fieldPort.getText()));
					data.setTypeOf("addWorker");
					master.tell(data);
					}
				
			}
		});
		btnAddWorker.setBounds(23, 86, 110, 23);
		panel.add(btnAddWorker);
		
		JLabel lblResults = new JLabel("Results: ");
		lblResults.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblResults.setBounds(23, 303, 56, 22);
		panel.add(lblResults);
		
		JLabel lblCpuTime = new JLabel("CPU Time:");
		lblCpuTime.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblCpuTime.setBounds(23, 372, 85, 14);
		panel.add(lblCpuTime);
		
		JLabel lblTimeSpent = new JLabel("Time spent:");
		lblTimeSpent.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblTimeSpent.setBounds(23, 397, 85, 22);
		panel.add(lblTimeSpent);
		
		JLabel lblSum = new JLabel("Sum of rho-cycles");
		lblSum.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblSum.setBounds(23, 336, 110, 14);
		panel.add(lblSum);
		
		results = new Label("");
		results.setBounds(97, 303, 387, 22);
		panel.add(results);
		
		sum = new Label("");
		sum.setBounds(143, 337, 325, 14);
		panel.add(sum);
		
		cpu = new Label("");
		cpu.setBounds(141, 372, 153, 14);
		panel.add(cpu);
		
		time = new Label("");
		time.setBounds(141, 401, 164, 14);
		panel.add(time);
		
		JLabel lblWorkers = new JLabel("Workers");
		lblWorkers.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblWorkers.setBounds(509, 7, 85, 23);
		panel.add(lblWorkers);
		
		final JButton btnDeleteWorker = new JButton("Delete worker");
		btnDeleteWorker.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				data.setAddress(fieldAddress.getText());
				data.setPort(Integer.parseInt(fieldPort.getText()));
				data.setTypeOf("delWorker");
				master.tell(data);
			}
		});
		btnDeleteWorker.setBounds(143, 86, 127, 23);
		panel.add(btnDeleteWorker);
		
		JButton btnStopFact = new JButton("Stop Factorization");
		btnStopFact.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				data.setTypeOf("stopFact");
				master.tell(data);
			}
		});
		btnStopFact.setBounds(23, 249, 192, 23);
		panel.add(btnStopFact);
		
		for(int i=0; i<nbworker; i++){
			final JLabel newlabel = new JLabel("-");
			lblsWorkers.add(newlabel);
			newlabel.setBounds(470, 50+(i*50), 120, 14);
			panel.add(newlabel);			
		}
		
	}
	
	//Set the labels when a new worker is added
	public void setListWorkers(ArrayList<GUIdata> workersdata) {
		int i=0;
		for(JLabel label: lblsWorkers)
		{
			label.setText("-");
		}
		for(GUIdata workerData: workersdata)
		{
			lblsWorkers.get(i).setText(workerData.getAddress() + ": " + Integer.toString(workerData.getPort()));
			i++;
		}
		
	}
	
	public void setLabelsResults(String primeFactors, String cpuTime, String sum, String time)
	{
		results.setText(primeFactors);
		cpu.setText(cpuTime);
		this.sum.setText(sum);
		this.time.setText(time);
	}
	
	public void setValuesLabels(){
		
	}
}
